package com.xuan.common.core.domain;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.xuan.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;


@Getter
@Setter
public class TableDataInfo {

    /**
     *  消息状态码
     */
    private int code;

    /**
     *  消息内容
     */
    private String msg;

    /**
     *  返回列表数据
     */
    private List<?> rows;

    /**
     *  总记录数
     */
    private long total;


    public static TableDataInfo success(List<?> rows) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(ResultCode.SUCCESS.getCode());
        tableDataInfo.setMsg(ResultCode.SUCCESS.getMessage());
        long total = 0;
        if (CollUtil.isEmpty(rows)) {
            rows = new ArrayList<>();
        } else {
            total = new PageInfo(rows).getTotal();
        }
        tableDataInfo.setRows(rows);
        tableDataInfo.setTotal(total);
        return tableDataInfo;
    }


    // 查询出的数据为空
    public static TableDataInfo empty() {
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(ResultCode.SUCCESS.getCode());
        tableDataInfo.setMsg(ResultCode.SUCCESS.getMessage());
        tableDataInfo.setRows(new ArrayList<>());
        tableDataInfo.setTotal(0);
        return tableDataInfo;
    }

    // 查询数据不为空时
    public static TableDataInfo success(List<?> rows, long total) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(ResultCode.SUCCESS.getCode());
        tableDataInfo.setMsg(ResultCode.SUCCESS.getMessage());
        tableDataInfo.setRows(rows);
        tableDataInfo.setTotal(total);
        return tableDataInfo;
    }
}
